package com.lanou3g.demo.controller;

import com.lanou3g.demo.entity.Cost;
import com.lanou3g.demo.service.ICostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//json数据的注解
//@RestController

@Controller
@RequestMapping("/cost")
public class CostController {
    @Autowired
    private ICostService costService;

    /**
     * 查询所有cost信息
     * @return
     */
    @RequestMapping("/findAll")
    //json数据注解
    @ResponseBody
    public List<Cost> findAll(){
        return costService.findAll();
    }

    /**
     * 添加cost信息
     * @param cost
     * @return
     */
    @RequestMapping("/addCost")
    public void addCost( Cost cost, HttpServletResponse resp) throws IOException {
//        String costName = req.getParameter("costName");
//        String baseDuration = req.getParameter("baseDuration");
//        String baseCost = req.getParameter("baseCost");
//        String unitCost = req.getParameter("unitCost");
//        String descr = req.getParameter("descr");
//         cost.setName(costName);
            int addCost = costService.addCost(cost);
            resp.sendRedirect("/kobe/fee/fee_list.html");

        }



    /**
     * 修改cost信息 根据costId修改
     * @param cost
     * @return
     */
    @RequestMapping("/upCost")
    public void updateCost(Cost cost, HttpServletResponse resp) throws IOException {

        Integer updateCost = costService.updateCost(cost);
        resp.sendRedirect("/kobe/fee/fee_list.html");


    }
    //修改状态

    @RequestMapping("/upStatus")
    @ResponseBody
    public Integer upStatus(HttpServletRequest request){
        String num = request.getParameter("num");
        System.out.println(Integer.valueOf(num));
        Integer upStatus = costService.upStatus(Integer.valueOf(num));
        return upStatus;
    }
//    @RequestMapping("/delOne")
//    public Integer delOne(HttpServletRequest request){
//        String num1 = request.getParameter("num");
    //        System.out.println(Integer.valueOf(num1));
//        Integer delOne = costService.delOne(Integer.valueOf(num1));
//         return delOne;
//    }

    /**
     * 删除 cost的信息
     * @param
     * @return
     */
    @RequestMapping("/delOne")
    @ResponseBody
    public Integer delOne(Cost cost) {
        Integer costId = cost.getCostId();
        System.out.println(costId);
        Integer delOne = costService.delOne(costId);
        return delOne;
    }
    @RequestMapping("/bdDesc")
    @ResponseBody
    public List<Cost> bdDesc(){
        return costService.bdDesc();
    }
    @RequestMapping("/bdAsc")
    @ResponseBody
    public List<Cost> bdAsc(){
        return costService.bdAsc();
    }
    @RequestMapping("/bcDesc")
    @ResponseBody
    public List<Cost> bcDesc(){
        return costService.bcDesc();
    }
    @RequestMapping("/bcAsc")
    @ResponseBody
    public List<Cost> bcAsc(){
        return costService.bcAsc();
    }

}
